﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Obvious.Framework.Notification.ConfigHandlers;
using Obvious.Framework.Notification.Abstracts;
using System.Reflection;
using System.IO;

namespace Obvious.Framework.Notification.Factory
{
    internal class CustomNotificationProviderActivator
    {
        internal static NotificationProvider Activate(NotificationProviderElement element)
        {
            NotificationProvider provider = null;
            string strPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace(@"file:\", "");
            string customTypeString = element.CustomType;
            if (!string.IsNullOrEmpty(customTypeString))
            {
                string[] arrTypes = customTypeString.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
                string type = arrTypes[0];
                string assemblyName = string.Empty;
                if (arrTypes.Length > 1)
                    assemblyName = arrTypes[1];
                try
                {
                    Assembly assembly = System.Reflection.Assembly.LoadFrom(Path.Combine(strPath,assemblyName));
                    Type customType = assembly.GetType(type);
                    ConstructorInfo constructorInfo = customType.GetConstructor(Type.EmptyTypes);
                    provider = (NotificationProvider)constructorInfo.Invoke(new Object[0]);
                    provider.ConfigName = element.Name;
                }
                catch
                {
                }
            }
            return provider;
        }
    }
}
